﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//using WB.Entity;
//using WB.DaoHelper;
using WB.Library;
using Bussiness.Manager;
using Bussiness.Entity;

public partial class Admin_News_NewsList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {      
        if (!IsPostBack)
        {
            Pager1.ChangePage += new EventHandler(Pager1_ChangePage);
            LoadCategory();
            LoadData();
        }
    }

    void Pager1_ChangePage(object sender, EventArgs e)
    {
        LoadData();
    }

    void LoadCategory()
    {
     
        string catType = "NEWS";
        if (Request["control"] == "branch")
        {
            catType = "BRAN";
        }
        else
        {
            ddlCategory.Items.Clear();
            ddlCategory.Items.Add(new ListItem("Tất cả", ""));
        }
        DataTable dtCat = CmsCategoryManager.CreateInstant().GetCategoryTreeByLang("NEWS", "vi-VN");
            ddlCategory.DataSource = dtCat;
            ddlCategory.DataTextField = "CatName";
            ddlCategory.DataValueField = "CategoryId";
            ddlCategory.DataGroupField = "CatName";
            ddlCategory.DataBind();
       
    }
    public string GetEditUrl(object id)
    { 
         if (Request["control"] == "branch")
         {
             return "?control=addbranch&id=" + id;
         }
         return "?control=NucNews&id=" + id;
        
    }
    void LoadData()
    {

        DataTable dt = null;
        if (Request["control"] == "branch")
        {
            dt = CmsNewsManager.CreateInstant().SearchBranch((ddlCategory.SelectedValue.ToString() == "" ? Guid.Empty : new Guid(ddlCategory.SelectedValue.ToString())), txtTitle.Text.Trim());
        }
        else
        {
            dt = CmsNewsManager.CreateInstant().SearchNews((ddlCategory.SelectedValue.ToString() == "" ? Guid.Empty : new Guid(ddlCategory.SelectedValue.ToString())), txtTitle.Text.Trim());
        }
        if (dt!= null && dt.Rows.Count >= 0)
        {
            Pager1.DataSource = dt.DefaultView;
            Pager1.BindToControl = rep;
            rep.DataSource = dt;
            rep.DataBind();
        }


    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        LoadData();
    }

    protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "del":
                CmsNewsInfosManager.CreateInstant().DeleteByNewsId(new Guid(e.CommandArgument.ToString()));
                CmsNewsManager.CreateInstant().DeleteById(new Guid(e.CommandArgument.ToString()));
                LoadData();
                break;
        }
    }


    protected void btnAddNews_Click(object sender, EventArgs e)
    {
        if (Request["control"] == "branch")
        {
            Response.Redirect("?control=addbranch");
        }
        else
        {
            Response.Redirect("?control=NucNews");
        }
        
    }

}
